// _Ticker.cpp: implementation of the _Ticker class.
//
//////////////////////////////////////////////////////////////////////

#include "_Ticker.h"

#include "qstring.h"
#include "qsize.h"
#include "qevent.h"
#include "qpainter.h"

//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////

_Ticker::_Ticker(QWidget *parent) : Ticker (parent)
{
	offset = 0;
	myTimerId = 0;
}

_Ticker::~_Ticker()
{

}

void _Ticker::setText (const QString &newText)
{
	myText = newText;
}

QSize _Ticker::sizeHint () const
{
	return fontMetrics ().size (0, text ());
}

void _Ticker::paintEvent (QPaintEvent *event)
{
	QPainter painter (this);
	int textWidth = fontMetrics ().width (text ());
	if (textWidth < 1)
		return;
	int x = -offset;
	while (x < width ()) {
		painter.drawText (x, 0, textWidth, height (), Qt::AlignLeft|Qt::AlignCenter, text ());
		x += textWidth;
	}
}

void _Ticker::hideEvent (QHideEvent *event)
{
	killTimer (myTimerId);
	myTimerId = 0;
}

void _Ticker::timerEvent (QTimerEvent *event)
{
	if (event->timerId () == myTimerId) {
		++offset;
		if (offset >= fontMetrics ().width (text ()))
			offset = 0;
//		update ();
		scroll (-1, 0);	//	scroll(0, 0) will not send a paint event
	}
	else {
		QWidget::timerEvent (event);
	}
}

void _Ticker::showEvent (QShowEvent *event)
{
	myTimerId = startTimer (30);
}
